﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HumanResources
{
    public class Employee : TaxEntity
    {
        #region attributes
        private static readonly float FICA_RATE;
        static Employee()
        {
            FICA_RATE = .0765f;
        }

        float payrate;
        float ytdSalary=0; 
        float ytdTaxes=0;
        #endregion

       

        public Employee(String name, String ssn)
        : this(name, ssn, 11)
        {
           
        }
        public Employee(String name, String ssn, float salary)
            : base(name, ssn)
        {
            Salary = salary;
        }

        #region properties
        //public string Name
        //{
        //    set
        //    {
        //        if (value != null && value.Length < 30 && value.Length > 2)
        //            name = value;
        //    }
        //    get
        //    {
        //        return name;
        //    }
        //}

        public float Salary
        {
            get { return payrate; }
            set
            {
                if (value > 10 && value < 1000)
                {
                    payrate = value;
                }
                else
                {
                    throw new ValidationException("Salary", 10, 1000, value);
                }
            }
        }


        protected override string TaxIdRegularExpression
        {
            get
            {
                return @"^\d\d\d-\d\d-\d\d\d\d$";
            }
        }

        [Obsolete("Use TaxId")]
        public string SocialSecurityNumber
        {
            get { return TaxId; }
            set
            {
                TaxId = value;
//                if (value != null && value.Length == 11)
//                    ssn = value;
            }
        }

        public float YtdSalary
        {
            get { return ytdSalary; }
        }

        public float YtdTax
        {
            get { return ytdTaxes; }
        }
        
        #endregion
        #region methods

        /// <summary>
        /// Pays this  employee their salary.
        /// </summary>
        /// <example>
        /// <code>
        /// Employee e = new Employee();
        /// ...
        /// float net = e.Pay();
        /// </code>
        /// </example>
        /// <returns>net pay after taxes.</returns>
        /// 

        public override float Pay()
        {
            return Pay(Salary);
        }
        public float Pay (float amount)
        {
            if (amount > 10 && amount < 1000)
            {
                base.Pay();
                float tax = amount * FICA_RATE;
                ytdSalary += amount;
                ytdTaxes += tax;
                return amount - tax;
            }
            return 0;
        }
        //public float Pay(params float[] amounts)
        //{
        //    if (amounts == null || amounts.Length == 0)
        //    {
        //       return Pay();               
        //    }
        //    float totalNet = 0;
        //    foreach (float amount in amounts)
        //    {
        //        totalNet += Pay(amount);
        //    }
        //    return totalNet;
        //}

        public static float operator + (Employee emp, float amount)
        {
            return emp.Pay(amount);
        }

        // not really going to use this, so don't want to open this can of worms
        //public static bool operator == (Employee emp1, Employee emp2)
        //{
        //    if (object.ReferenceEquals(emp1, emp2))
        //        return true;
        //    if (object.ReferenceEquals(emp1,null) || object.ReferenceEquals(emp2, null))
        //        return false;
        //    return emp1.SocialSecurityNumber == emp2.SocialSecurityNumber;
        //}
        //public static bool operator != (Employee emp1, Employee emp2)
        //{
        //    return !(emp1 == emp2);
        //}

        public override string ToString()
        {
            return Name + " - " + TaxId + " Ytd Salary: " + YtdSalary;
        }
        #endregion
    }
}
